<? 
	include('base.php');
	User::protect();
	$uo2 = User::get($_GET['id']);
	$u = User::getCurrent();
	
	switch($_GET['task']) {
		case 'update':
			$res = $uo2->update($_POST);
			if (!db::isError($res)) {
				header('Location: admin_user_edit.php?id=' . $_GET['id']);
			}
			break;
		case 'deactivate':
			$res = $uo2->deactivate();
			if (!db::isError($res)) {
				header('Location: admin_user_edit.php?id=' . $_GET['id']);
			}
			break;
		case 'activate':
			$res = $uo2->activate();
			if (!db::isError($res)) {
				header('Location: admin_user_edit.php?id=' . $_GET['id']);
			}
			break;
		case 'delete':
			$res = $uo2->remove();
			if (!db::isError($res)) {
				header('Location: admin_users.php');
			}
			break;
	}
	
	$calendar = true;
	$section = 'admin_users';
	$page_title = 'Edit User';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="admin.php">Administer Audition&nbsp;&#62;</a>&nbsp;<a href="admin_users.php">Users&nbsp;&#62;</a>&nbsp;Edit User
</div>

<? 
$u = User::getCurrent();
if (!$u->isAdmin()) { 
	Error::outputDialog('Return to Main Menu', 'index.php', 'Only an administrator may access administrator options.');

} else if (db::isError($uo2)) {
	
	$uo2->outputDialog('Return to Users','admin_users.php');

} else { 

	if ($res) { // it's an error
		if (db::isError($res)) {
			$res->outputList();
		}
	}

	$email = ($_GET['task'] == 'update') ? $_POST['email'] : $uo2->getEmailAddress();
	$birthdate = ($_GET['task'] == 'update') ? $_POST['birthdate'] : $uo2->getBirthDate();
	$lastname = ($_GET['task'] == 'update') ? $_POST['lastname'] : $uo2->getLastName();
	$firstname = ($_GET['task'] == 'update') ? $_POST['firstname'] : $uo2->getFirstName();
	$level = ($_GET['task'] == 'update') ? $_POST['level'] : $uo2->getLevel();
	
	if ($_GET['id'] && ($_GET['task'] == 'edit' || $_GET['task'] == 'update')) { ?>
	
		<h1>edit user:</h1>
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>?task=update&amp;id=<?=$_GET['id']?>" method="post">
		<table border="0" cellspacing="0" cellpadding="0" class="edit-form">
		<tr>
			<td width="50%"><strong>Username</strong><br/><?=$uo2->getUserName()?></td>
			<td width="50%"><strong>Email Address</strong><br/><input type="text" name="email" value="<?=$email?>" class="text"></td>
		</tr>
		<tr>
			<td><strong>Password</strong><br/><input type="password" name="password" value="" class="text" /></td>
			<td><strong>Password (Confirm)</strong><br/><input type="password" name="password_confirm" value="" class="text" /></td>
		</tr>
		<tr>
			<td colspan="2">Leave the password blank for it to remain unchanged.</td>
		</tr>
		<tr>
			<td><strong>Date of Birth (<a style="vertical-align: middle" href="javascript:void(0)" onclick="popUpCalendar(this, document.getElementById('date'), '<?=DATE_JS_CALENDAR?>')">Calendar</a>)</strong><br/>
				<input type="text" name="birthdate" id="date" value="<?=$birthdate?>" class="text"></td>
			<td><strong>User Level</strong>
			<select name="level">
				<option value="USER"<? if ($level == 'USER') { ?> selected<? } ?>>Regular User</option>
				<option value="ADMIN"<? if ($level == 'ADMIN') { ?> selected<? } ?>>Administrator</option>
			</select>
			</td>
		</tr>
		<tr>
			<td><strong>Last&nbsp;Name:<br/><input type="text" name="lastname" value="<?=$lastname?>" class="text"></td>
			<td><strong>First Name</strong><br/><input type="text" name="firstname" value="<?=$firstname?>" class="text"></td>
		</tr>
		</table>
		
		
		<div class="edit-form">
			<input type="submit" value="Update User" name="submit" class="button">
			<input type="submit" value="Exit Without Updating" name="preview" onclick="location.href='<?=$PHP_SELF?>?id=<?=$uo2->getID()?>'; return false" class="button">
		</div>
		
	
	<? } else { ?>

		<h1>view user:</h1>
		<div class="inset">
		
		<h2><?=$uo2->getFirstName()?> <?=$uo2->getLastName()?></h2>
		<span class="small">
			Status: <? if ($uo2->isActive()) { ?><b>Active</b><? } else { ?><b>Inactive</b><? } ?><? if ($uo2->getBirthDate() != '0000-00-00') { ?><span class="padded">|</span>DOB: <?=$uo2->getBirthDate()?><? } ?><? if ($uo2->getEmailAddress()) { ?><span class="padded">|</span><a href="mailto:<?=$uo2->getEmailAddress()?>"><?=$uo2->getEmailAddress()?></a><? } ?><span class="padded">|</span>Access Level: <strong><?=$uo2->getLevel()?></strong>
			
		</span>
		<br><br>
		
		<form name="entry_status" method="get">
			<input type="submit" name="" value="Edit" onclick="location.href='<?=$PHP_SELF?>?task=edit&id=<?=$_GET['id']?>';return false" class="button">
			<? if ($uo2->isActive()) { ?><input type="submit" name="" value="Deactivate" onclick="location.href='<?=$PHP_SELF?>?task=deactivate&id=<?=$_GET['id']?>';return false" class="button"><? } else { ?><input type="submit" name="" value="Activate" onclick="location.href='<?=$PHP_SELF?>?task=activate&id=<?=$_GET['id']?>';return false" class="button"><? } ?>
			<input type="button" value="Delete" name="" onclick="if (confirm('Are you sure you wish to delete this user?\rDoing so may compromise some release information.\rWhenever possible, deactivate rather than delete.')) { location.href='<?=$PHP_SELF?>?task=delete&id=<?=$_GET['id']?>'};return false" class="button">
			<input type="button" value="Return to Users" name="" onclick="location.href='admin_users.php'; return false" class="button">
		</form>
		
		</div>
	<? } ?>


<? } ?>

<? include('layout/footer.php'); ?>
